package com.sbr.esbmonitor.wicket.pages.queues;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.esbmonitor.activemq.model.Queue;
import com.sbr.esbmonitor.activemq.repository.jmx.impl.JMXQueueRepository;
import com.sbr.esbmonitor.repositories.xml.impl.XMLServerRepository;
import com.sbr.esbmonitor.servicemix.Server;
import com.sbr.esbmonitor.wicket.pages.BasePage;
import com.sbr.esbmonitor.wicket.pages.queues.messages.ListMessagesPage;

public class ViewQueuePage extends BasePage {

	private static final Logger LOG = LoggerFactory.getLogger(ViewQueuePage.class);

	public ViewQueuePage(final PageParameters params) {
		
		
		final Long serverId = params.getLong("serverId");
		LOG.debug("serverId = {}",serverId);
		
		final String queueName = params.getString("queueName");
		LOG.debug("queueName = {}",queueName);
		
		final Server server = new XMLServerRepository().selectById(serverId);
		
		
		IModel<Queue> model = new LoadableDetachableModel<Queue>() {

			private static final long serialVersionUID = 1L;

			@Override
			protected Queue load() {
				return new JMXQueueRepository().selectByName(queueName, server);
			}
		};
		
		Queue queue = model.getObject();
				
		add(new Label("name", queue.getName()));
		add(new Label("consumerCount",""+queue.getConsumerCount()));
		add(new Label("size", ""+queue.getQueueSize()));
		
		add(new BookmarkablePageLink<ListMessagesPage>("browse", ListMessagesPage.class, params));
	}
}
